﻿using Anke.SHManage.BLL.FixedAssetsBLL;
using Anke.SHManage.Model.FixedAssetsModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Anke.SHManage.Web.Areas.FixedAssets.Controllers
{
    /// <summary>
    /// 固定资产设备基础信息
    /// plq
    /// </summary>
    public class FixedAssetsMaterialController : Controller
    {
        //
        // GET: /FixedAssets/FixedAssetsMaterial/

        #region 固定资产列表

        /// <summary>
        /// 固定资产列表视图页
        /// </summary>
        /// <returns></returns>
        public ActionResult AssetsMaterialList()
        {
            return View("AssetsMaterialList");
        }

        /// <summary>
        /// 获取固定资产信息列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAssetsList()
        {
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求的页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //获取查询条件
            string assetName = Request.Form["assetName"]; //资产名称
            string assetID = Request.Form["assetID"];    //固定资产编码
            string assetTypeID = Request.Form["assetTypeID"].ToString(); //资产分类编码
            bool isScrap = Convert.ToBoolean(Request.Form["isScrap"]); //是否报废
            string assetBrand = Request.Form["assetBrand"];   //资产品牌
            string strAssetCurStorageID = Request.Form["assetCurStorageID"].ToString();
            int assetCurStorageID = (strAssetCurStorageID == "") ? -1 : Convert.ToInt32(Request.Form["assetCurStorageID"]); //当前仓库(使用部门/分站)编码
            string assetManageDep = Request.Form["assetManageDep"];       //资产管理部门
            string ambulanceNum = Request.Form["ambulanceNum"];           //存放车辆牌号

            FixedAssetsMaterialBLL bll = new FixedAssetsMaterialBLL();
            var list = bll.GetAssetsList(pageSize, pageIndex, assetName, assetID, assetTypeID, isScrap, assetBrand, assetCurStorageID, assetManageDep, ambulanceNum);
            return Json(list, "appliction/json", JsonRequestBehavior.AllowGet);

        }

        //删除单条数据
        public ActionResult DeleteData(string Code)
        {
            string str = "";
            if (new FixedAssetsMaterialBLL().DeleteData(Code))
            {
                str = "删除成功";
            }
            else
                str = "删除失败";
            return Json(str, JsonRequestBehavior.AllowGet);

        }

        #endregion

        #region 固定资产新增/修改

        /// <summary>
        /// 固定资产新增/编辑视图页
        /// </summary>
        /// <param name="State"></param>
        /// <param name="Code"></param>
        /// <returns></returns>
        public ActionResult AssetsInfoEdit(string State, string Code)
        {
            ViewBag.State = State;
            ViewBag.Code = Code;
            return View("AssetsInfoEdit");
        }

        /// <summary>
        /// 获取编辑页初始数据
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public ActionResult GetEditPageData(string Code)
        {
            FixedAssetsMaterialBLL bll = new FixedAssetsMaterialBLL();
            FIXED_MatetialInfo info = bll.GetEditPageData(Code);
            return Json(info, JsonRequestBehavior.AllowGet);
        }

        //编辑页保存
        public ActionResult SaveEditPageData(FIXED_MatetialInfo info)
        {
            string str = "";
            string state = Request.Form["state"].ToString();
            FixedAssetsMaterialBLL bll = new FixedAssetsMaterialBLL();
            switch (state)
            {
                case "edit":
                    if (bll.UpdateEditPageData(info))
                    {
                        str = "更新成功";
                    }
                    else
                    {
                        str = "更新失败";
                    }
                    break;
                case "new":
                    //验证该固定资产编码是否已有对应数据
                    if (!String.IsNullOrEmpty(info.GuDingZiChanBianMa) && bll.IsHaveCurrentData(info.GuDingZiChanBianMa))
                    {
                        str = "对应固定资产编码已有资产数据";
                    }
                    else {
                        if (bll.InsertEditPageData(info))
                        {
                            str = "新增成功";
                        }
                        else
                        {
                            str = "新增失败";
                        }
                    }

                    break;
            }
            return Json(str, "text/html", JsonRequestBehavior.AllowGet);
        }


        #endregion



    }
}
